﻿Ext.namespace("GridAuthInfo");
GridAuthInfo.List = function() {
	this.txtRoleName = new Ext.form.TextField({
		width:100
	});
	this.txtEntityName = new Ext.form.TextField({
		width:150
	});
	this.cmbLevel=new Ext.form.ComboBox({
		width : 120,
        store:OMS.GRID_LEVEL,
        typeAhead:true,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'请选择级别',
        selectOnFocus: true	
	});
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查询',
        tooltip: {text: '查询'},
        handler: this.onSearch,
        scope: this
    });
    this.btnAdd = new Ext.Toolbar.Button({
     	text:'增加', 
     	scope:this, 
     	iconCls:'icon-add', 
     	handler:this.onAdd
    });
    this.btnModify = new Ext.Toolbar.Button({
    	text:'修改', 
    	scope:this, 
    	iconCls:'icon-modify', 
    	handler:this.onModify
    });
    this.btnDelete = new Ext.Toolbar.Button({
    	text:'删除', 
    	scope:this,
    	iconCls:'icon-delete', 
    	handler:this.onDelete
    });
    
    this.cm = new Ext.grid.ColumnModel({
		defaults:{
			width:40,
			sortable:true,
			align:'center'
		},
		columns:[
			new Ext.grid.RowNumberer(),
			{id:'gridAuthId',header:'编号',dataIndex:'GRID_AUTH_ID',hidden:true},
			{header:'角色名称',dataIndex:'ROLE_NAME',width:80},
			{header:'功能名称',dataIndex:'ENTITY_NAME',width:50},
			{header:'访问级别',dataIndex:'AUTH_LEVEL',renderer:this.renderLevel,width:100}
		]
	});
    this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url:'../gridauth/list.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'gridAuthId',
			fields: ['GRID_AUTH_ID','ENTITY_NAME','ROLE_ID','ROLE_NAME','ENTITY_ID','AUTH_LEVEL']
		}),
		listeners:{
			 beforeload:{
				fn:function(thiz,options){
					Ext.apply(thiz.baseParams,{
						role_name :this.txtRoleName.getValue(),
						entity_name :this.txtEntityName.getValue(),
						auth_level :this.cmbLevel.getValue()
					});
				},
				scope:this
			}
		}
	});
	this.grid = new Ext.grid.GridPanel({
		title: '网格访问权限列表',
		region: 'center',
		id:'gridAuthGrid',
		store:this.store,
		height:455,
		cm:this.cm,
		scope:this,
		loadMask: {msg: '加载中，请稍候...'},
		tbar: ['角色名称:',this.txtRoleName,'功能名称:',this.txtEntityName,'访问级别',this.cmbLevel,this.btnSearch, '->',this.btnAdd/*,this.btnModify*/,this.btnDelete],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		loadMask: {msg:'加载中，请稍候...'},
		bbar:new Ext.PagingToolbar({
			pageSize:15,
			store: this.store,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条信息，一共 {2} 条',
			emptyMsg: '当前没有社区记录'
		}),
		viewConfig:{forceFit:true},
		listeners:{
//	      	    rowdblclick : function(grid ,rowIndex ,e){
//	      	    	var data = grid.store.getAt(rowIndex).data;	//json
//		      	    if(GridAuthInfo.Detail.instance==null){
//						GridAuthInfo.Detail.instance=new GridAuthInfo.Detail();
//					}
//					GridAuthInfo.Detail.instance.initEdit(data);
//					GridAuthInfo.Detail.instance.show();
//				},
//				scope:this				
	       }
	});
	GridAuthInfo.List.superclass.constructor.call(this,{	
		resizable: false,
	    frame:false,	
		items:[this.grid]
	});
	//this.on('dblclick',this.onModify,this);
	this.store.load();
};
Ext.extend(GridAuthInfo.List, Ext.Viewport, {
	onSearch:function(){
		this.store.load({
			params:{
				start :0,
				limit :15,
				role_name :this.txtRoleName.getValue(),
				entity_name :this.txtEntityName.getValue(),
				auth_level :this.cmbLevel.getValue()
			}
		});
	},
	renderLevel : function(value, attr, record) {
		if(value !=null&&value !=''){
			return OMS.GRID_LEVEL[value][1];
		}
		return '';
	},
	onAdd:function(){
		if(GridAuthInfo.Detail.instance==null){
			GridAuthInfo.Detail.instance=new GridAuthInfo.Detail();
		}
		GridAuthInfo.Detail.instance.initAdd();
		GridAuthInfo.Detail.instance.show();
	},
	onModify:function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return;
		}
		var data=record.data;
		if(GridAuthInfo.Detail.instance==null){
			GridAuthInfo.Detail.instance=new GridAuthInfo.Detail();
		}
		GridAuthInfo.Detail.instance.initEdit(data);
		GridAuthInfo.Detail.instance.show();
	},
	onDelete:function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return;
		}
		Ext.MessageBox.confirm("提示","确认删除  '"+record.data.ROLE_NAME+"'的此项权限 ？",function(btn){
            if(btn=="yes"){
				Ext.Ajax.request({
					url: '../gridauth/delete.form?timesnap=' + Date.parse(new Date()),
					method: 'POST',
					scope:this,
					params: {
						grid_auth_id :record.data.GRID_AUTH_ID
					},
					success:function(resp){	
						this.store.reload();
					},
					failure:function(resp){	
						Utils.info('操作失败!','系统提示');
						return;
					}
				});
			}else{}
		},this);
	}
});

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget='side';
	Ext.BLANK_IMAGE_URL='../resources/images/default/s.gif';
	//Ext.lib.Ajax.defaultPostHeader+=";charset=utf-8";//初始化编码
	GridAuthInfo.List.instance=new GridAuthInfo.List();
	GridAuthInfo.List.instance.show();
});